package com.hcz.util;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.apache.commons.dbcp.BasicDataSource;



public class DbHelp<T> {
	
	private static String driver ; 
	private static String url ;
	private static String username ;
	private static String password ;
	private static BasicDataSource datasource ;
	static{
		Properties p = new Properties();
		try {
			p.load(DbHelp.class.getClassLoader().getResourceAsStream("db.properties"));
			driver = p.getProperty("jdbc.driver");
			url = p.getProperty("jdbc.url");
			username = p.getProperty("jdbc.user");
			password = p.getProperty("jdbc.password");
			
			datasource = new BasicDataSource();
			datasource.setDriverClassName(driver);
			datasource.setUrl(url);
			datasource.setUsername(username);
			datasource.setPassword(password);
			datasource.setMaxActive(20);
			datasource.setMaxWait(5000);
			datasource.setInitialSize(5);
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	

	/**
	 *建立数据库连接
	 */
	public Connection getConnections(){
		
		try {
			return datasource.getConnection();
		}  catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * �ͷ�����
	 * @param rs
	 * @param stat
	 * @param conn
	 */
	public void relieveLink(ResultSet rs ,PreparedStatement stat ,Connection conn){
		try {
			if(rs != null){
			rs.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				if(stat != null){
				stat.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}finally {
				try {
					if(conn != null){
					conn.close();
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
				
			}
			
		}
		
	}
	/**
	 * ������ݿ�
	 * @param sql
	 * @param name
	 * @param password
	 */
	public int  executeUpdateSql(String sql, Object...args) {
		Connection conn = getConnections();
		PreparedStatement stat = null;
		int num  = 0;
		try {
			 stat = conn.prepareStatement(sql);
			 for(int i = 0; i <args.length ; i ++){
				 stat.setObject(i+1,args[i]);
			 }
			 num = stat.executeUpdate();
		}catch(Exception e) {
			e.printStackTrace();
		}finally{
			relieveLink(  null, stat , conn);
			
		}
		return num;
	}
	/**
	 * ��ѯ���������ĵ����û���¼
	 * @param sql
	 * @param args
	 * @return
	 */
	public T exeSelectForObject(String sql ,ResultSetPackge<T> packge,Object...args){
		Connection conn = getConnections();
		PreparedStatement stat = null;
		ResultSet rs = null;
		T t = null;
		try {
			 stat = conn.prepareStatement(sql);
			 for(int i = 0; i <args.length ; i ++){
				 stat.setObject(i+1,args[i]);
			 }
			 rs = stat.executeQuery();
			 if(rs.next()){
				 t = packge.packgeResultSet(rs);
			 }
		
		}catch(Exception e) {
			e.printStackTrace();
		}finally{
			relieveLink(  rs, stat , conn);
		
		
		}
		return  t;
	}
	/**
	 * ���������û�
	 * @param sql
	 * @param packgeUser
	 * @return
	 */
	public List<T> exeSelectForList(String sql, ResultSetPackge<T> packge,Object...args) {
		Connection conn = getConnections();
		PreparedStatement stat = null;
		ResultSet rs = null;
		List<T>list = new ArrayList<T>();
		T t = null;
		try {
			 stat = conn.prepareStatement(sql);
			 for(int i = 0; i <args.length ; i ++){
				 stat.setObject(i+1,args[i]);
			 }
			 rs = stat.executeQuery();
			while(rs.next()){
				 t = packge.packgeResultSet(rs);
				 list.add(t);
			 }
		
		}catch(Exception e) {
			e.printStackTrace();
		}finally{
			relieveLink(  rs, stat , conn);
		
		
		}
		return  list;
		
	}
	
	
}
